﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Agents.Admin
{
    public partial class AgentsReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            panelIncome.Visible = (radReportType.SelectedValue == "Profit");
            panelWorkingHours.Visible = (radReportType.SelectedValue == "Hours");
        }

        /// <summary>
        /// Occurs when the Report type changes and re-binds the results
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void radReportType_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelIncome.Visible = (radReportType.SelectedValue == "Profit");
            panelWorkingHours.Visible = (radReportType.SelectedValue == "Hours");
        }

        /// <summary>
        /// Redirecting the admin back to his home page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminMenu.aspx");
        }

        /// <summary>
        /// Takes care of the page indexing of the DataGrid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dgWorkingHours_OnPageIndexChanged(object sender, DataGridPageChangedEventArgs e)
        {
            // Changing the data grid viewing to the selected index
            dgWorkingHours.CurrentPageIndex = e.NewPageIndex;

            // Binding the data grid data
            dgWorkingHours.DataBind();
        }

        /// <summary>
        /// Occurs when a row is bound, and sums up the total hours
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dgWorkingHours_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            // Make the sum zero in the beginning of the binding
            if (e.Item.ItemType == ListItemType.Header)
            {
                lblTotal.Text = "0";
            }

            // Add the current row to the sum of hours
            if ((e.Item.ItemType == ListItemType.Item) || 
                (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                double rowTotal = Convert.ToDouble(DataBinder.Eval(e.Item.DataItem, "Mhours"));
                lblTotal.Text = (Convert.ToDouble(lblTotal.Text) + rowTotal).ToString("F");
            }
        }
    }
}